﻿using System;
using System.Globalization;

namespace NazTek.Extension.Clr4
{
    /// <summary> 
    /// Extends int
    /// </summary> 
    /// <remarks></remarks> 
    public static class IntExtension
    {
        /// <summary> 
        /// Converts to an item in the generically specified Enum type, based on value match 
        /// </summary> 
        /// <typeparam name="T">The Generic type to search for the corresponding item match</typeparam> 
        /// <param name="enumValue">Corresponding integer value of the enum item to search for</param> 
        /// <returns>The matched enum item</returns> 
        /// <remarks></remarks> 
        /// <example>
        /// <code>
        /// // context
        /// internal enum Gender
        /// {
        ///     Male,
        ///     Female
        /// }
        /// 
        /// // usage
        /// Enum e = 1.ToEnum&lt;Gender&gt;(false); // Female
        /// Enum e = 2.ToEnum&lt;Gender&gt;(false); // null
        /// Enum e = 2.ToEnum&lt;Gender&gt;(true); // Exception
        /// </code>
        /// </example>
        public static T ToEnum<T>(this int enumValue)
        {
            Parameter.ValidateToEnum<int, T>(enumValue, Common.QTN_INT);
            return (T)Enum.Parse(typeof(T), enumValue.ToString(CultureInfo.InvariantCulture));
        }
    }
}